/*
	Copyright (C) 2011 Daniel C. Dennison (dandennison84@gmail.com).  All rights reserved.
	This file is licensed under the EPL 1.0 (Eclipse Public License 1.0)
	A copy of the license can be found at http://www.eclipse.org/legal/epl-v10.html
*/

package com.gmail.dandennison84.accounting

import org.junit.Before
import org.junit.Test

class BalanceSheetTest {
	List<Account> coa
	Map<String, List<Account>> incomeStatement
	Map<String, List<Account>> retainedEarningsStatement
	JournalHelper jh
	
	@Before
	void before() {
		coa = new ChartOfAccountsHelper().chartOfAccounts
		jh = new JournalHelper()
		incomeStatement = jh.getIncomeStatement(coa)
		StatementBuilder reb = new StatementBuilder(coa)	
		reb.retainedEarnings {
			equityAccounts(accountType:AccountType.EQUITY)
		}
		retainedEarningsStatement = reb.accounts
	}

	@Test
	void testBalanceSheet() {
		StatementBuilder bs = new StatementBuilder(coa)
		bs.balanceSheet {
			assetAccounts(accountType:AccountType.ASSETS)
			liabilitiesAccounts(accountType:AccountType.LIABILITIES)
		}
		Map<String, List<Account>> bsStatement = bs.accounts
		assert 2 == bsStatement.size()
		assert 13 == bsStatement['assetAccounts'].size()
		assert 7 == bsStatement['liabilitiesAccounts'].size()
		double netIncome = jh.getNetIncome(incomeStatement)
		double equity = jh.getRetainedEarnings(retainedEarningsStatement)
		double assetBalance = bsStatement['assetAccounts'].inject(0) {
			acc, it -> acc + (it.debitBalance - it.creditBalance)
		}
		double liabilityBalance = bsStatement['liabilitiesAccounts'].inject(0) {
			acc, it -> acc + (it.creditBalance - it.debitBalance)
		}
		assert assetBalance == (equity + liabilityBalance + netIncome)
	}
}
